﻿using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using static System.Net.Mime.MediaTypeNames;

namespace AyuBookmark.Common.Models
{
    /// <summary>
    /// 书签实体类，表示用户保存的网页书签信息
    /// </summary>
    [Table("bookmarks")]
    public class Bookmark
    {

        /// <summary>
        /// 最后验证时间
        /// </summary>
        [Column("LastValidated")]
        public DateTime? LastValidated { get; set; }

        /// <summary>
        /// 是否无效标记
        /// </summary>
        [Column("IsInvalid")]
        public bool IsInvalid { get; set; }

        /// <summary>
        /// 无效原因
        /// </summary>
        [Column("InvalidReason")]
        [StringLength(200)]
        public string InvalidReason { get; set; }

        // 新增最后访问时间字段
        public DateTime? LastAccessed { get; set; }

        /// <summary>
        /// 图片路径（非数据库字段）
        /// </summary>
        [NotMapped]
        public string ImagePath { get; set; }

        /// <summary>
        /// 分类名称（非数据库字段）
        /// </summary>
        [NotMapped]
        public string CategoryName { get; set; }

        /// <summary>
        /// 分类颜色（非数据库字段）
        /// </summary>
        [NotMapped]
        public string CategoryColor { get; set; }


        /// <summary>
        /// 主键ID，自增长
        /// </summary>
        [Key]
        [Column("id")]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }

        /// <summary>
        /// URL的SHA256哈希值，用于快速查重
        /// </summary>
        [Required]
        [Column("url_hash")]
        [StringLength(64)]
        public string UrlHash { get; set; }

        /// <summary>
        /// 完整的网页URL地址
        /// </summary>
        [Required]
        [Column("url")]
        [DataType(DataType.Url)]
        public string Url { get; set; }

        /// <summary>
        /// 网页标题
        /// </summary>
        [Required]
        [Column("title")]
        [StringLength(500)]
        public string Title { get; set; }

        /// <summary>
        /// 网页内容摘要
        /// </summary>
        [Column("description")]
        public string Description { get; set; }

        /// <summary>
        /// 关联的首图ID
        /// </summary>
        [Column("image_id")]
        public int? ImageId { get; set; }

        /// <summary>
        /// 导航属性 - 首图
        /// </summary>
        [ForeignKey("ImageId")]
        public virtual Image Image { get; set; }

        /// <summary>
        /// 关联的分类ID
        /// </summary>
        [Column("category_id")]
        public int? CategoryId { get; set; }

        /// <summary>
        /// 导航属性 - 分类 
        /// </summary>
        [ForeignKey("CategoryId")]
        public virtual Category Category { get; set; }

        /// <summary>
        /// 创建时间，自动设置为当前时间
        /// </summary>
        [Column("created_time")]
        [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
        public DateTime CreatedTime { get; set; } = DateTime.Now;

        /// <summary>
        /// 最后修改时间，更新时自动设置为当前时间
        /// </summary>
        [Column("last_modified")]
        public DateTime LastModified { get; set; } = DateTime.Now;

        /// <summary>
        /// 计算属性 - 获取简化的域名信息
        /// </summary>
        [NotMapped]
        public string Domain
        {
            get
            {
                if (Uri.TryCreate(Url, UriKind.Absolute, out var uri))
                {
                    return uri.Host.Replace("www.", "");
                }
                return "unknown";
            }
        }

        /// <summary>
        /// 更新最后修改时间为当前时间
        /// </summary>
        public void Touch()
        {
            LastModified = DateTime.Now;
        }

        /// <summary>
        /// 验证书签基本信息的有效性
        /// </summary>
        /// <returns>验证结果</returns>
        public bool Validate()
        {
            return !string.IsNullOrWhiteSpace(Url) &&
                   !string.IsNullOrWhiteSpace(Title) &&
                   Uri.IsWellFormedUriString(Url, UriKind.Absolute);
        }

        /// <summary>
        /// 重写ToString方法，返回有意义的书签信息
        /// </summary>
        public override string ToString()
        {
            return $"{Title} ({Url})";
        }
    }
}